package com.wj;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jun.wang
 * @title: FileUtil
 * @projectName ownerpro
 * @description: TODO
 * @date 2019/9/20 14:31
 */
public class FileUtil {

    public static List<PhoneNumber> read() {
        List<PhoneNumber> list = new ArrayList<>();
        InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream("countryPhoneCode.dat");
        if (inputStream == null) {
            return null;
        }
        try {
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line = bufferedReader.readLine();
            while (line != null && line.length() != 0) {
                String[] arr = line.split("#");
                PhoneNumber phoneNumber = new PhoneNumber();
                phoneNumber.setNameEn(arr[0]);
                phoneNumber.setNameChinese(arr[1]);
                phoneNumber.setCountryCode(arr[2]);
                phoneNumber.setPhoneCode(arr[3]);
                phoneNumber.setDiffTime(arr[4]);
                list.add(phoneNumber);
                line = bufferedReader.readLine();
            }
        }
        catch (Exception e) {
            throw new RuntimeException("read phone number file error", e);
        }
        finally {
            try {
                inputStream.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
